package com.example.zsf.view;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.zsf.R;


public class NoNetWorkView extends LinearLayout {
    private ViewGroup container;

    public NoNetWorkView(Context context, ViewGroup container, NoNetWorkListener listener) {
        super(context);
        this.container = container;
        initView(context, listener);
    }

    private void initView(final Context context, final NoNetWorkListener listener) {
        View view = inflate(context, R.layout.view_no_network, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        view.setLayoutParams(layoutParams);
        view.setEnabled(false);
        addView(view);

        view.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //重新加载也走一遍
                NoNetWorkView.checkNetWork(context, container, listener);
            }
        });
    }

    /**
     * 判断是否有网
     *
     * @param context   .
     * @param container 添加到哪的容器
     * @param listener  回调监听
     * @return NoNetWorkView的实例, 如果返回的是null就代表着参数不合法或者网络可用.
     */
    public static NoNetWorkView checkNetWork(Context context, ViewGroup container, final NoNetWorkListener listener) {
        if (context == null || container == null) {
            return null;
        }
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = null;
        if (connectivityManager != null) {
            networkInfo = connectivityManager.getActiveNetworkInfo();
        }

        if (networkInfo != null && networkInfo.isAvailable()) {
            //可用的时候放弃添加NoNetWorkView
            for (int i = container.getChildCount() - 1; i >= 0; i--) {
                View view = container.getChildAt(i);
                if (view.getTag() != null && view.getTag().equals("NoNetWorkView")) {
                    container.removeViewAt(i);
                    break;
                }
            }
            if (listener != null) {
                listener.normalProcess();
            }
            return null;
        }

        View findNoNetWorkView = null;
        for (int i = container.getChildCount() - 1; i >= 0; i--) {
            View view = container.getChildAt(i);
            if (view.getTag() != null && view.getTag().equals("NoNetWorkView")) {
                findNoNetWorkView = view;
            }
        }

        if (findNoNetWorkView != null) {
            return (NoNetWorkView) findNoNetWorkView;
        }
        NoNetWorkView noNetWorkView = new NoNetWorkView(context, container, listener);
        noNetWorkView.setTag("NoNetWorkView");
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        container.addView(noNetWorkView, container.getChildCount() - 1, layoutParams);
        return noNetWorkView;
    }


    public interface NoNetWorkListener {
        /**
         * 如果有网的话做什么操作
         */
        void normalProcess();
    }
}
